package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

    @Autowired
    private UserAccountMapper userAccountMapper;
    @Resource
    private UserAccountDetailMapper userAccountDetailMapper;

    @Override
    public void register(Long userId) {
        //	user_account
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userId);
        userAccount.setTotalAmount(new BigDecimal("1000.00"));
        userAccount.setAvailableAmount(new BigDecimal("1000.00"));
        userAccountMapper.insert(userAccount);
    }

    @Override
    public BigDecimal getAvailableAmount(Long userId) {
        // 根据用户ID获取用户可用余额
        LambdaQueryWrapper<UserAccount> userAccountLambdaQueryWrapper = new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId);
        BigDecimal availableAmount = userAccountMapper.selectOne(userAccountLambdaQueryWrapper).getAvailableAmount();
        return availableAmount;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean checkAndDeduct(AccountDeductVo accountDeductVo) {
        //  执行update语句
        int result = userAccountMapper.checkAndUpdate(accountDeductVo.getAmount(), accountDeductVo.getUserId());
        if (result > 0){
            //记录当前资金明细
            addUserAccountDetal(accountDeductVo.getAmount(), accountDeductVo.getUserId(), accountDeductVo.getContent(), accountDeductVo.getOrderNo(), SystemConstant.ACCOUNT_TRADE_TYPE_MINUS);
            return true;
        }
        return false;
    }

    /**
     * 添加用户资金明细
     * @param amount
     * @param userId
     * @param content
     * @param orderNo
     * @param accountTradeTypeMinus
     */
    public void addUserAccountDetal(BigDecimal amount, Long userId, String content, String orderNo, String accountTradeTypeMinus) {
        //创建对象
        UserAccountDetail userAccountDetail = new UserAccountDetail();
        userAccountDetail.setUserId(userId);
        userAccountDetail.setAmount(amount);
        userAccountDetail.setTitle(content);
        userAccountDetail.setOrderNo(orderNo);
        userAccountDetail.setTradeType(accountTradeTypeMinus);
        userAccountDetailMapper.insert(userAccountDetail);
    }

    @Override
    public void addUserAccountDetail(Long userId, String content, String typeMinus, BigDecimal amount, String orderNo) {
        //	创建对象
        UserAccountDetail userAccountDetail = new UserAccountDetail();
        userAccountDetail.setUserId(userId);
        userAccountDetail.setTitle(content);
        userAccountDetail.setTradeType(typeMinus);
        userAccountDetail.setAmount(amount);
        userAccountDetail.setOrderNo(orderNo);
        userAccountDetailMapper.insert(userAccountDetail);
    }

    @Override
    public IPage<UserAccountDetail> findUserRechargePage(Page<UserAccountDetail> detailPage, Long userId) {
        //	构建查询条件;
        LambdaQueryWrapper<UserAccountDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAccountDetail::getUserId, userId).eq(UserAccountDetail::getTradeType, SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT).orderByDesc(UserAccountDetail::getId);
        return userAccountDetailMapper.selectPage(detailPage,wrapper);
    }

    @Override
    public IPage<UserAccountDetail> findUserConsumePage(Page<UserAccountDetail> detailPage, Long userId) {
        //	构建查询条件;
        LambdaQueryWrapper<UserAccountDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAccountDetail::getUserId, userId).eq(UserAccountDetail::getTradeType, SystemConstant.ACCOUNT_TRADE_TYPE_MINUS).orderByDesc(UserAccountDetail::getId);
        return userAccountDetailMapper.selectPage(detailPage,wrapper);
    }
}
